package com.csu.cphone.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.params.geo.GeoRadiusParam;

import com.csu.common.map.BaiduMapHelpper;
import com.csu.common.utils.RedisUtil;
import com.csu.common.vo.MapResultVo;
import com.csu.cphone.constants.RedisKey;
import com.csu.cphone.dao.CityMapper;
import com.csu.cphone.dao.StoreMapper;
import com.csu.cphone.pojo.CityPojo;
import com.csu.cphone.pojo.StorePojo;
import com.csu.cphone.service.LoactionService;
import com.csu.cphone.vo.AdressVo;

/**
 * @项目名称：cphone
 * @类名称：StoreServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月12日 下午2:40:35
 * @company:
 */
@Service
public class LoactionServiceImpl implements LoactionService
{
    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CityMapper cityMapper;

    /**
     * @see com.csu.cphone.service.LoactionService#getNearlistStore(double,
     *      double)
     */
    @Override
    public List<StorePojo> getNearlistStore(double lng, double lat)
    {

        if (Double.valueOf(0).equals(lng) && Double.valueOf(0).equals(lat))
        {
            return getDefalutStores();
        }

        List<StorePojo> storeList = new ArrayList<StorePojo>();
        double radius = 100d;
        GeoRadiusParam param = GeoRadiusParam.geoRadiusParam().withDist().sortAscending().count(5);
        List<GeoRadiusResponse> resultList = RedisUtil.getRedisDb().georadius(getKey(), lng, lat, radius * 2,
                GeoUnit.KM, param);

        List<Integer> idList = new ArrayList<Integer>();
        for (GeoRadiusResponse result : resultList)
        {
            String member = result.getMemberByString();
            Integer id = Integer.parseInt(member);
            idList.add(id);
        }

        if (CollectionUtils.isEmpty(idList))
        {
            return getDefalutStores();
        }
        else
        {
            storeList = storeMapper.selectByIdList(idList);
        }

        return storeList;
    }

    /**
     * @description
     * @author TangC
     * @date 2017年12月12日 下午10:12:38
     * @return
     */
    private List<StorePojo> getDefalutStores()
    {
        List<StorePojo> result = new ArrayList<StorePojo>();
        // TODO
        result = storeMapper.getLast5Stores();
        return result;
    }

    /**
     * @see com.csu.cphone.service.LoactionService#addStore(com.csu.cphone.pojo.StorePojo)
     */
    @Override
    public StorePojo addStore(String number, Double lng, Double lat, String name, String address, Integer producerId)
    {
        StorePojo store = new StorePojo();
        store.setAddress(address);
        store.setLat(lat);
        store.setLng(lng);
        store.setName(name);
        store.setNumber(number);
        store.setProducerId(producerId);
        storeMapper.insert(store);
        RedisUtil.getRedisDb().geoadd(getKey(), lng, lat, getMember(store.getId()));
        return store;
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月27日 下午4:39:12
     * @param id
     * @return
     */
    private byte[] getMember(Integer id)
    {
        return String.valueOf(id).getBytes();
    }

    /**
     * @description
     * @author TangC
     * @date 2017年11月27日 下午4:38:18
     * @return
     */
    private byte[] getKey()
    {
        return RedisKey.STORE_POSITION.getBytes();
    }

    /**
     * @see com.csu.cphone.service.LoactionService#getAdreessByPosition(double,
     *      double)
     */
    @Override
    public AdressVo getAdreessByPosition(double lat, double lng)
    {
        AdressVo adressVo = new AdressVo();

        MapResultVo mapResult = BaiduMapHelpper.position2Address(lat, lng);

        adressVo.setAdress(mapResult.getFormatted_address());

        if (mapResult.getAddressComponent() != null)
        {
            adressVo.setCity(mapResult.getAddressComponent().getCity());
            adressVo.setProvince(mapResult.getAddressComponent().getProvince());
        }
        return adressVo;
    }

    /**
     * @see com.csu.cphone.service.LoactionService#getCityList()
     */
    @Override
    public List<CityPojo> getCityList()
    {
        return cityMapper.selectAll();
    }

    /**
     * @see com.csu.cphone.service.LoactionService#getPositionByAddreess(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public String getPositionByAddreess(String address, String city)
    {
        
        return BaiduMapHelpper.address2Position(address, city);
    }

}
